package nabaixin.cn;

import java.io.File;

import nabaixin.cn.crop.CropImage;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class TextCrop extends Activity {
	private final int REQUEST_FOR_CROPIMAGE = 1;
	
	private ImageView iv ;
	private Button b;
	
	@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.crop);
			
			b = (Button) findViewById(R.id.button1);
			iv = (ImageView) findViewById(R.id.imageView1);
			
			b.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					crop();
				}
			});
		}
	
	private void crop() {
        try {
        	File f = new File(Environment.getExternalStorageDirectory(), "sunset.jpg");
            Intent intent = new Intent(this, CropImage.class);
            Bundle extras = new Bundle();
            // if we want to scale
            // extras.putInt("outputX", 200);
            // extras.putInt("outputY", 200);
            // extras.putBoolean("scale", true);
            intent.setDataAndType(Uri.fromFile(f), "image/jpeg");
            intent.putExtras(extras);
            startActivityForResult(intent, REQUEST_FOR_CROPIMAGE);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode!=RESULT_OK)
			return;
		if(requestCode==REQUEST_FOR_CROPIMAGE){
			Bitmap b = data.getExtras().getParcelable("data");
			iv.setImageBitmap(b);
		}
	}
}
